<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审批状态" prop="projectState">
        <el-select v-model="queryParams.projectState" placeholder="请选择审批状态" clearable :style="{width: '100%'}">
          <el-option
            v-for="dict in dict.type.review_state"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:projectExpert:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectExpertList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="reviewId"/>
      <el-table-column label="专家负责项目id" align="center" prop="projectId"/>
      <el-table-column label="审批类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.review_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="projectState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.review_state" :value="scope.row.projectState"/>
        </template>
      </el-table-column>
      <el-table-column label="处理意见" align="center" prop="comment"/>
      <el-table-column label="审批结果" align="center" prop="result">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.review_result" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleProjectInfo(scope.row)"
            v-hasPermi="['system:projectExpert:edit']"
          >查看详情并附审核意见
          </el-button>
          <el-button
            v-if="scope.row.projectState === 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleApproved(scope.row)"
            v-hasPermi="['system:projectExpert:remove']"
          >通过
          </el-button>
          <el-button
            v-if="scope.row.projectState === 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDenied(scope.row)"
            v-hasPermi="['system:projectExpert:remove']"
          >否决
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目申报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="项目类别" prop="projectType">
          <el-select v-model="form.projectType" placeholder="请选择您的项目类别" clearable :style="{width: '100%'}"
                     :disabled="true">
            <el-option
              v-for="dict in dict.type.project_type"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目方向" prop="direction">
          <el-select v-model="form.direction" placeholder="请选择您的方向" clearable :style="{width: '100%'}" :disabled="true">
            <el-option
              v-for="dict in dict.type.exp_direction"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name" :readonly="true"/>
        </el-form-item>
        <el-form-item label="项目实施地点" prop="location">
          <el-input v-model="form.location" :readonly="true"/>
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable size="small"
                          v-model="form.startDate"
                          type="date"
                          :disabled="true"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable size="small"
                          v-model="form.endDate"
                          type="date"
                          :disabled="true"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经济行业领域" prop="economicField">
          <el-input v-model="form.economicField" readonly/>
        </el-form-item>
        <el-form-item label="细分领域1">
          <el-input v-model="form.fieldOne" readonly/>
        </el-form-item>
        <el-form-item label="细分领域2">
          <el-input v-model="form.fieldTwo" readonly/>
        </el-form-item>
        <el-form-item label="项目总投资" prop="investment">
          <el-input v-model="form.investment" readonly/>
        </el-form-item>
        <el-form-item label="申请专项经费" prop="specialMoney">
          <el-input v-model="form.specialMoney" readonly/>
        </el-form-item>
        <el-form-item label="项目负责人" prop="leader">
          <el-input v-model="form.leader" readonly/>
        </el-form-item>
        <el-form-item label="合作单位id" prop="cooperationId">
          <el-input v-model="form.cooperationId" readonly/>
        </el-form-item>
        <el-form-item label="主要研究内容与目标">
          <editor v-model="form.researchContent" :min-height="192" readOnly/>
        </el-form-item>
        <el-form-item label="创新点与关键技术" prop="innovationPoint">
          <el-input v-model="form.innovationPoint" type="textarea" placeholder="请输入内容" readonly/>
        </el-form-item>
        <el-form-item label="现有基础和实施条件" prop="basics">
          <el-input v-model="form.basics" type="textarea" readonly/>
        </el-form-item>
        <el-form-item label="预期结果与经济、社会效益" prop="expectation">
          <el-input v-model="form.expectation" type="textarea" readonly/>
        </el-form-item>
        <el-form-item label="考核指标" prop="assessIndicators">
          <el-input v-model="form.assessIndicators" type="textarea" readonly/>
        </el-form-item>
        <el-form-item label="主要研究人员id" prop="researchers">
          <el-input v-model="form.researchers" readonly/>
        </el-form-item>
        <el-form-item label="项目可行性报告">
          <file-upload v-model="form.feasibilityReport"/>
        </el-form-item>
        <el-form-item label="附件清单">
          <file-upload v-model="form.attachment"/>
        </el-form-item>
        <el-form-item label="审核专家名" prop="reviewer">
          <el-input v-model="form.reviewer" readonly/>
        </el-form-item>
        <el-form-item label="审核意见" prop="reviewResult">
          <el-input v-model="form.comment" type="textarea"/>
        </el-form-item>
        <el-form-item label="项目进度计划" prop="projectPlan">
          <el-input v-model="form.projectPlan" type="textarea" readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认审核意见</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listProjectExpert,
    getProjectExpert,
    delProjectExpert,
    addProjectExpert,
    updateProjectExpert
  } from "@/api/system/projectExpert";
  import {getProjectInfo} from "@/api/system/projectInfo";
  import {addProjectInfo, updateProjectInfo} from "../../../api/system/projectInfo";

  export default {
    name: "ProjectExpert",
    dicts: ['review_state', 'review_type', 'project_state', 'exp_direction', 'project_type', 'project_stage', 'review_result'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 项目评审表格数据
        projectExpertList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: this.$store.state.user.userId,
          type: null,
          projectState: null,
          result: null,
          comment: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          comment: [{required: true, message: '请输入审核意见', trigger: 'blur'}]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      typeFormatter(row, column) {
        return this.selectDictLabel(this.dict.type.project_type, row.projectType);
      },
      dirFormatter(row, column) {
        return this.selectDictLabel(this.dict.type.exp_direction, row.direction);
      },
      projectStateFormatter(row, column) {
        return this.selectDictLabel(this.dict.type.project_state, row.state);
      },
      stateFormatter(column) {
        return this.selectDictLabel(this.dict.type.project_state, column);
      },
      stageFormatter(row, column) {
        return this.selectDictLabel(this.dict.type.project_stage, row.stage);
      },
      reviewStateFormatter(row, column) {
        return this.selectDictLabel(this.dict.type.review_state, row.projectState);
      },
      reviewTypeFormatter(row, column) {
        return this.selectDictLabel(this.dict.type.review_type, row.type);
      },
      resultFormatter(row, column) {
        return this.selectDictLabel(this.dict.type.review_type, row.result);
      },

      /** 查询项目评审列表 */
      getList() {
        this.loading = true;
        listProjectExpert(this.queryParams).then(response => {
          this.projectExpertList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      // 表单重置
      reset() {
        this.form = {
          reviewId: null,
          projectId: null,
          projectType: null,
          direction: null,
          name: null,
          location: null,
          startDate: null,
          endDate: null,
          economicField: null,
          fieldOne: null,
          fieldTwo: null,
          investment: null,
          specialMoney: null,
          leader: null,
          cooperationId: null,
          researchContent: null,
          innovationPoint: null,
          basics: null,
          expectation: null,
          assessIndicators: null,
          researchers: null,
          feasibilityReport: null,
          attachment: null,
          submitTime: null,
          state: null,
          stage: null,
          reviewer: null,
          comment: null,
          projectPlan: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.expertId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 否决 */
      handleDenied(row) {
        row.result = 2; //不通过
        row.createBy = this.$store.state.user.name;
        updateProjectExpert(row).then(response => {
          this.$modal.msgSuccess("已否决");
          this.getList();
        });
      },
      /** 通过申请 */
      handleApproved(row) {
        row.result = 1; // 通过
        row.createBy = this.$store.state.user.name;
        updateProjectExpert(row).then(response => {
          this.$modal.msgSuccess("已通过");
          this.getList();
        });
      },
      /** 查看项目详情 */
      handleProjectInfo(row) {
        getProjectInfo(row.projectId).then(response => {
          this.form = response.data;
          this.form.reviewId = row.reviewId;
          this.open = true;
          this.title = '项目详情资料';
        })
      },
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateProjectExpert(this.form).then(response => {
              this.$modal.msgSuccess("已添加审核结果");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/projectExpert/export', {
          ...this.queryParams
        }, `projectExpert_${new Date().getTime()}.xlsx`)
      }
    }
  }
  ;
</script>
